package com.example.controller;

import com.example.Vo.employeeVO;
import com.example.service.IemployeeService;
import com.example.util.PageResult;
import com.example.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.entity.employee;
import java.util.Arrays;
import java.util.List;


    @RestController
    @RequestMapping("/employee")
    public class employeeController {

        @Autowired
        private IemployeeService employeeService;

        // 员工列表请求
        @RequestMapping("/list")
        public R getemployee() {
            System.out.println(employeeService.list());
            return R.ok().data("list",employeeService.list());
        }

        // 分页查询请求
        @GetMapping("/findByPage/{page}/{pageSize}")
        public R findByPage(@PathVariable("page") Integer page,
                            @PathVariable("pageSize") Integer pageSize) {
            PageResult<employee> PR = employeeService.findByPage(page,pageSize);
            return R.ok().data("PR",PR);
        }
        // 条件分页查询请求
        @PostMapping("/search/{page}/{pageSize}")
        public R search(@PathVariable("page") Integer page,
                        @PathVariable("pageSize") Integer pageSize,
                        @RequestBody(required = false) employeeVO vo){
            PageResult<employee> PR = employeeService.search(page,pageSize,vo);
            return R.ok().data("PR",PR);
        }
        //添加员工信息请求，从无到有用post
        @PostMapping("/add")
        public R add(@RequestBody employee employee){
            boolean judge=employeeService.add(employee);
            if(judge){
                return R.ok().message("添加员工信息成功!");
            }else
            {
                return R.ok().message("添加员工信息失败！");
            }
        }
        //更新员工信息请求，在已有的基础上进行更改用put
        @PutMapping("/update")
        public R update(@RequestBody employee employee){
            boolean judge=employeeService.updateById(employee);
            if(judge){
                return R.ok().message("修改员工信息成功!");
            }else
            {
                return R.ok().message("修改员工信息失败！");
            }
        }
        //删除员工信息请求，删除用delete
        @DeleteMapping("/delete/{Id}")
        public R delete(@PathVariable("Id")String id){
            String[] temp=id.split(",");//通过split这个函数将字符串数组用‘，’分割
            List<String> employeeId= Arrays.asList(temp);
            return employeeService.removeByIds(employeeId) ? R.ok().message("删除员工成功!") : R.error().message("删除员工失败!");
        }
}
